package cn.picclife.mwx.manager.sys.controller;

import cn.picclife.mwx.manager.sys.model.bo.*;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.service.SysMaintainService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@RestController
@RequestMapping("sysMaintain")
public class SysMaintainController {
	
	private Log log = LogFactory.getLog(SysMaintainController.class);

	@Autowired
	private SysMaintainService sysMaintainServiceImpl;
	
	/**
	 * 增加数据
	 * @param req
	 */
	@RequestMapping(value="/saveSysMaintain", method= {RequestMethod.POST})
	public PageResponse saveSysMaintain(@RequestBody SysMaintainReq req) {
		
		log.info("===========saveSysMaintain系统维护数据接口请求报文："+JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		try {
			sysMaintainServiceImpl.saveSysMaintain(req);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========saveSysMaintain系统维护数据接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========saveSysMaintain系统维护数据接口返回报文："+JSONObject.toJSON(pageResponse).toString());
		return pageResponse;
		
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/querySysMaintain", method= {RequestMethod.POST})
	public PageResponse querySysMaintain(@RequestBody SysMaintainReq req,HttpServletRequest request) {
		
		log.info("===========querySysMaintain查询系统数据量接口请求报文："+JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		try {

			PageCount pageCount = sysMaintainServiceImpl.querySysMaintain(req,request);
			pageResponse.setPageCount(pageCount);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========querySysMaintain查询系统数据量接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========querySysMaintain查询系统数据量接口返回成功=================");
		return pageResponse;
	}
	
	/**
	 * 数据更新
	 */
	@RequestMapping(value="/updateSysMaintain", method= {RequestMethod.POST})
	public DataResponse updateSysMaintain(@RequestBody SysMaintainReq req) {
		
		log.info("===========querySysMaintain查询系统数据量接口请求报文："+JSONObject.toJSON(req).toString());
		DataResponse dataResponse = new DataResponse();
		if(null==req||null==req.getId()||req.getId().trim().length()<=0) {
			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========saveCollect数据采集接口返回报文："+JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		try {
			int sum = sysMaintainServiceImpl.updateSysMaintain(req);
			if(sum==0) {
				dataResponse.setCode(ResponseStatusEnum.UPDATE_FAIL_CODE.getCode());
				dataResponse.setMsg(ResponseStatusEnum.UPDATE_FAIL_CODE.getName());
				log.info("===========querySysMaintain查询系统数据量接口返回报文："+JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
		} catch (Exception e) {
			log.error(e);
			dataResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			dataResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========querySysMaintain查询系统数据量接口返回报文："+JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========querySysMaintain查询系统数据量接口返回报文："+JSONObject.toJSON(dataResponse).toString());
		return dataResponse;
	}
	
	/**
	 * 文件上传
	 * @param
	 */
	/*@RequestMapping(value="/uploadFile", method= {RequestMethod.POST})
	public void uploadFile(@RequestBody MultipartFile file, HttpServletRequest request) {
		
		log.info("文件上传开始：");
		File newFile = new File(filePath);
		// 将内存中的数据写入磁盘
        try {
			file.transferTo(newFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			log.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error(e);
		}
		log.info("文件上传成功！");
	}*/
	/**
	 * 删除数据
	 */
	@RequestMapping(value="/deleteSysMaintain",method={RequestMethod.POST})
	public BaseResult1 deleteSysMaintain(@RequestBody SysMaintainReq req){
		log.info("============deleteSysMaintain删除系统数据量接口请求报文："+req.getDatas());
		
		BaseResult1 result = new BaseResult1();
//		if (StringUtils.isEmpty(datas)) {
//			result.setRet(BaseResult1.BIZ_ERROR);
//			result.setMsg("请选择后在点击删除按钮");
//			return result;
//		}
		try {
			String[] split = req.getDatas().split(",");
			List<String> list = Arrays.asList(split);
			int count = sysMaintainServiceImpl.deleteSysMaintain(list);
			if (count <1 ) {
				result.setRet(BaseResult1.BIZ_ERROR);
				result.setMsg("删除失败");
				return result;
			}else {
				result.setRet(BaseResult1.SUCCESS);
				result.setMsg("删除成功");
				return result;
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e);
			result.setRet(BaseResult1.SYS_ERROR);
			result.setMsg("删除失败");
			return result;
		}
		
	}
	
	/**
	 * 测试ftp服务器是否可以调通
	 * @param req
	 */
//	@RequestMapping(value="/testConnect", method= {RequestMethod.POST})
//	public DataResponse testConnect(@RequestBody SysMaintainReq req) {
//		log.info("===========log日志可以用"+JSONObject.toJSON(req).toString());
//		DataResponse dataResponse = new DataResponse();
//		if(null==req||null==req.getServerConStr()||req.getServerConStr().trim().length()<=0
//				||null==req.getPassword()||req.getPassword().length<=0) {
//			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
//			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
//			log.info("===========saveCollect数据采集接口返回报文："+JSONObject.toJSON(dataResponse).toString());
//			return dataResponse;
//		}
//		boolean result = false;
//		try {
//			result = sysMaintainServiceImpl.ftpConnect(req);
//		} catch (Exception e) {
//			e.printStackTrace();
//			dataResponse.setCode(ResponseStatusEnum.FAIL_CODE.getCode());
//			dataResponse.setMsg(ResponseStatusEnum.FAIL_CODE.getName());
//			log.info("===========测试ftp服务器接口返回报文："+JSONObject.toJSON(dataResponse).toString());
//			return dataResponse;
//		}
//		if(!result) {
//			dataResponse.setCode(ResponseStatusEnum.FAIL_CODE.getCode());
//			dataResponse.setMsg(ResponseStatusEnum.FAIL_CODE.getName());
//			log.info("===========测试ftp服务器接口返回报文："+JSONObject.toJSON(dataResponse).toString());
//			return dataResponse;
//		}
//		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
//		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
//		log.info("===========测试ftp服务器接口返回报文："+JSONObject.toJSON(dataResponse).toString());
//		return dataResponse;
//	}
	
}
